package com.demo.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.widget.model.ShowMode;
import com.demo.widget.model.ViewInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class TextAdapter extends NBaseAdapter {

	private LayoutInflater inflater;
	private List<? extends HashMap<String, ?>> mdata;
	private int mResource;

	private String[] from;
	private int[] to;

	public TextAdapter(Context context, List<? extends HashMap<String, ?>> data, int resource, String[] from, int[] to) {
		this.inflater = LayoutInflater.from(context);
		this.mdata = data;
		this.mResource = resource;
		this.from = from;
		this.to = to;
	}

	@Override
	public int getCount() {
		return mdata.size();
	}

	@Override
	public Object getItem(int position) {
		return mdata.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(mResource, null);
		}
		Map<String, ?> data = mdata.get(position);

		for (int i = 0; i < to.length; i++) {
			View view = convertView.findViewById(to[i]);
			bindView(view, data, from[i]);
		}

		ViewInfo viewInfo = new ViewInfo();
		viewInfo.setMode(ShowMode.GENERAL);
		convertView.setTag(viewInfo);
		
		return convertView;
	}

	private void bindView(View view, Map<String, ?> data, String key) {
		Object item = data.get(key);

		if (view instanceof TextView) {
			((TextView) view).setText(item == null ? "" : item.toString());
		}
	}

	@Override
	public ImageView getImageView(View convertView) {
		return null;
	}
}
